function parse(text) {
    var doc;
    if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(text);
    } else {
        var parser = new DOMParser();
        doc = parser.parseFromString(text, "text/xml");
    }
    return doc;
}

function parsePointsText(text) {
    return parsePoints(parse(text));
}

function parsePoints(doc) {
    var root = doc.documentElement;
    var points = new Points();

    for (var i = 0; i < root.childNodes.length; i++)
    {
        var child = root.childNodes[i];

        if ("flatList" == child.nodeName) {
            var flat = new Flat();
            flat.id = parseInt(child.getAttribute('id'));
            flat.userid = parseInt(child.getAttribute('userid'));
            flat.lng = parseFloat(child.getAttribute('lng'));
            flat.lat = parseFloat(child.getAttribute('lat'));

            flat.type = child.getAttribute('type');
            flat.address = child.getAttribute('address');
            flat.roomNumber = parseInt(child.getAttribute('roomNumber'));
            flat.square = parseInt(child.getAttribute('square'));
            flat.floor = parseInt(child.getAttribute('floor'));
            flat.price = parseFloat(child.getAttribute('price'));
            flat.currency = child.getAttribute('currency');
            flat.comment =  child.getAttribute('comment');
            
            points.flatList.push(flat);
        } else if ("officeList" == child.nodeName) {
            var office = new Office();
            office.id = parseInt(child.getAttribute('id'));
            office.userid = parseInt(child.getAttribute('userid'));
            office.lng = parseFloat(child.getAttribute('lng'));
            office.lat = parseFloat(child.getAttribute('lat'));

            office.type = child.getAttribute('type');
            office.address = child.getAttribute('address');
            office.roomNumber = parseInt(child.getAttribute('roomNumber'));
            office.square = parseInt(child.getAttribute('square'));
            office.floor = parseInt(child.getAttribute('floor'));
            office.price = parseFloat(child.getAttribute('price'));
            office.currency = child.getAttribute('currency');
            office.comment =  child.getAttribute('comment');

            points.officeList.push(office);
        }
    }

    return points;
}

//-----------------------------------------------------------------

function flat2Text(flat) {
    return "<points>" +
           "<flatList " +
           "id=\"" + flat.id + "\" " +
           "lat=\"" + flat.lat + "\" " +
           "lng=\"" + flat.lng +
           "\" />" +
           "</points>";
}

function office2Text(flat) {
    return "<points>" +
           "<officeList " +
           "id=\"" + flat.id + "\" " +
           "lat=\"" + flat.lat + "\" " +
           "lng=\"" + flat.lng +
           "\" />" +
           "</points>";
}